<?php
if(!isset($_POST['query']) || strlen(trim($_POST['query']))==0){
$myfile="data.txt";

$str="champion products ch approves stock split champion products inc said its board of directors approved a two for one stock split of its common shares for shareholders of record as of april the company also said its board voted to recommend to shareholders at the annual meeting april an increase in the authorized capital stock from five mln to mln shares reuter";
}
else 
{
$myfile=$_POST['dataset'];
$str=$_POST['query'];
}
function cosine_sa($tokensA, $tokensB)
{
    $a = $b = $c = 0;
    $uniqueTokensA = $uniqueTokensB = array();

    $uniqueMergedTokens = array_unique(array_merge($tokensA, $tokensB));

    foreach ($tokensA as $token) $uniqueTokensA[$token] = 0;
    foreach ($tokensB as $token) $uniqueTokensB[$token] = 0;

    foreach ($uniqueMergedTokens as $token) {
        $x = isset($uniqueTokensA[$token]) ? 1 : 0;
        $y = isset($uniqueTokensB[$token]) ? 1 : 0;
        $a += $x * $y;
        $b += pow($x,2);
        $c += pow($y,2);
    }
    return $b * $c != 0 ? $a / sqrt($b * $c) : 0;
}
function euclidean_sa($tokensA, $tokensB)
{
    $a = $b = $c = 0;
    $uniqueTokensA = $uniqueTokensB = array();

    $uniqueMergedTokens = array_unique(array_merge($tokensA, $tokensB));

    foreach ($tokensA as $token) $uniqueTokensA[$token] = 0;
    foreach ($tokensB as $token) $uniqueTokensB[$token] = 0;

    foreach ($uniqueMergedTokens as $token) {
        $x = isset($uniqueTokensA[$token]) ? 1 : 0;
        $y = isset($uniqueTokensB[$token]) ? 1 : 0;
        $a += $x * $y;
        $b += pow($x,2);
        $c += pow($y,2);
    }

	
    
	return $b * $c != 0 ? sqrt(($a)  / ($b + $c - $a)  ): 0;
}

function tanimoto_sa($tokensA, $tokensB)
{
    $a = $b = $c = 0;
    $uniqueTokensA = $uniqueTokensB = array();

    $uniqueMergedTokens = array_unique(array_merge($tokensA, $tokensB));

    foreach ($tokensA as $token) $uniqueTokensA[$token] = 0;
    foreach ($tokensB as $token) $uniqueTokensB[$token] = 0;

    foreach ($uniqueMergedTokens as $token) {
        $x = isset($uniqueTokensA[$token]) ? 1 : 0;
        $y = isset($uniqueTokensB[$token]) ? 1 : 0;
        $a += $x * $y;
        $b += pow($x,2);
        $c += pow($y,2);
    }

	
    
	return $b * $c != 0 ? ($a)  / ($b + $c - $a)  : 0;
}

function dice_sa($tokensA, $tokensB)
{
    $a = $b = $c = 0;
    $uniqueTokensA = $uniqueTokensB = array();

    $uniqueMergedTokens = array_unique(array_merge($tokensA, $tokensB));

    foreach ($tokensA as $token) $uniqueTokensA[$token] = 0;
    foreach ($tokensB as $token) $uniqueTokensB[$token] = 0;

    foreach ($uniqueMergedTokens as $token) {
        $x = isset($uniqueTokensA[$token]) ? 1 : 0;
        $y = isset($uniqueTokensB[$token]) ? 1 : 0;
        $a += $x * $y;
        $b += pow($x,2);
        $c += pow($y,2);
    }

	
    
	return $b * $c != 0 ? (2*$a)  / ($b + $c )  : 0;
}




$fh = fopen($myfile, "r") or exit("Unable to open file!");
//$file = fopen("outputs.txt", 'w') or die("can't open file");
$string=array();
while($inputs=fgets($fh)){array_push($string,trim($inputs)); $inputs=fgets($fh);  }
echo "<b>query</b> : $str <br>";
$text1=explode(' ',$str);
?>
<table border="1">
<tr>
<td> Data set </td>
<th>Oliver similiraity (php)</th>
<th>Cosine Similarity</th>
<th>Euclidian distance Similarity</th>
<th>Tanimoto Similarity</th>
<th>Dice Similarity</th>
</tr>
<?php
$i=1;
foreach ($string as $x)
{
echo "<tr><td>$i</td>";
$i++; 
$text2=explode(' ',$x);
similar_text($str,$x,$y);
echo "<td>$y </td>";
$y=cosine_sa($text1,$text2);
$y*=100;
echo "<td>$y </td>";
$y=euclidean_sa($text1,$text2);
$y*=100;
echo "<td>$y </td>";
$y=tanimoto_sa($text1,$text2);
$y*=100;
echo "<td>$y </td>";
$y=dice_sa($text1,$text2);
$y*=100;
echo "<td>$y </td></tr>";

}





//print_r($string);
fclose($fh);
//fclose($file);
?>